<?php
session_start();
if (isset($_SESSION["username"])) {
?>
  <?php require_once('common/header.php'); ?>
  <!--页面主要内容-->
  <main class="lyear-layout-content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-toolbar clearfix">
             <form class="pull-right search-bar" method="get" action="" role="form">
              <div class="input-group">
                <div class="input-group-btn">
                  <input type="hidden" name="search" id="search" value="name">
                  <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                    姓名查询 <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu">
                    <!--<li> <a tabindex="-1" href="javascript:void(0)" data-field="title">渠道来源</a> </li>-->
                    <li> <a tabindex="-1" href="javascript:void(0)" data-field="name">姓名查询</a> </li>
                    <li> <a tabindex="-1" href="javascript:void(0)" data-field="phone">收货人号码</a> </li>
                    <li> <a tabindex="-1" href="javascript:void(0)" data-field="order_id">订单号查询</a> </li>
                    <li> <a tabindex="-1" href="javascript:void(0)" data-field="goods">商品名称查询</a> </li>
                  </ul>
                </div>
                <input type="text" class="form-control" value="" name="keyword" placeholder="请输入查询信息">
              </div>
            </form>

              <div class="toolbar-btn-action">
                <a class="btn btn-primary m-r-5" href="PHPexcel.php"><i class="mdi mdi-plus"></i>导出订单</a>
                 <!--<a class="btn btn-info m-r-5" href="#"><i class="mdi mdi-poll"></i>打印订单</a>-->
                 <a class="btn btn-info m-r-5" href="controller/GteApista.php"><i class="mdi mdi-cube-outline"></i>同步订单状态</a>
              </div>
              
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <form name="form1" id="form1" method="post" action="controller/deleteall.php">

                <table class="table table-bordered">
                  <?php
                  include_once("../untils/conn.php");
                  mysqli_query($con, "set names utf8");
                  if ($con) {
                    if ($db) {
                      //获取数据总行数
                      $sql = "SELECT oder.id,oder.oderid, oder.channel, oder.goods, oder.proxy_id, oder.name, oder.uid, oder.phone, oder.sf, oder.city, oder.xian, oder.dizhi, oder.status, oder.beizhu, oder.time, oder.card_sta, channel.channel_name 
                              FROM oder 
                              LEFT JOIN channel 
                              ON oder.channel = channel.channel_id";
                      
                      // 根据查询方式和查询信息筛选订单数据
                      if (isset($_GET['search']) && isset($_GET['keyword'])) {
                        $search = $_GET['search'];
                        $keyword = $_GET['keyword'];
                        if ($search == 'name') {
                          $sql .= " WHERE oder.name LIKE '%$keyword%'";
                        } elseif ($search == 'phone') {
                          $sql .= " WHERE oder.phone LIKE '%$keyword%'";
                        } elseif ($search == 'order_id') {
                          $sql .= " WHERE oder.oderid LIKE '%$keyword%'";
                        }elseif ($search == 'goods') {
                          $sql .= " WHERE oder.goods LIKE '%$keyword%'";
                        }
                      }
                      
                      $sql .= " ORDER BY oder.id DESC";
                      $data = mysqli_query($con, $sql);
                      $maxrows = mysqli_num_rows($data);
                      //计算总页数
                      $page_size = 10;  //每页显示数
                      if ($maxrows % $page_size == 0) {
                        $maxpage = (int)($maxrows / $page_size);
                      } else {
                        $maxpage = (int)($maxrows / $page_size) + 1;
                      }
                      //获取当前页
                      if (isset($_GET['curpage'])) {
                        $page = $_GET['curpage'];
                      } else {
                        $page = 1;
                      }
                      //分段取出数据
                      $start = $page_size * ($page - 1);
                      $get_sql = $sql . " LIMIT $start,$page_size";
                      //得到数据显示
                      $data = mysqli_query($con, $get_sql);
                  ?>
                      <thead>
                        <tr>
                          <th>订单ID</th>
                          <th>订单号</th>
                          <th>订单来源</th>
                          <th>商品名称</th>
                          <th>姓名</th>
                          <th>身份证号</th>
                          <th>手机号码</th>
                          <th>省份</th>
                          <th>城市</th>
                          <th>县区</th>
                          <th>详细地址</th>
                          <th>处理状态</th>
                          <th>号码状态</th>
                          <th>备注</th>
                          <th>下单时间</th>
                
                          <th>操作</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        while ($row = mysqli_fetch_array($data)) {
                        ?>
                          <tr>
                            <td style="width:5%;">
                              <?php echo $row["id"] ?>
                            </td>
                            <td style="width:5%;">
                              <?php echo $row["oderid"] ?>
                            </td>
                            
                           <td style="width:7%;">
                                <?php 
                                    if (isset($row["proxy_id"]) && $row["proxy_id"] != null) {
                                        echo "代理商:".$row["proxy_id"];
                                    } else {
                                        if ($row["channel_name"]) {
                                            echo $row["channel_name"];
                                        } else {
                                            if ($row["channel"] == null || $row["channel"] == 0) {
                                                echo "官方渠道";
                                            } else {
                                                echo $row["channel"];
                                            }
                                        }
                                    }
                                ?>
                            </td>

                            <td style="width:7.5%;"><?php echo $row["goods"] ?>
                            </td>
                            <td style="width:5%;"><?php echo $row["name"] ?>
                            </td>
                            <td style="width:5%;"><?php echo $row["uid"] ?>
                            </td>
                            <td style="width:5%;"><?php echo $row["phone"] ?>
                            </td>
                            <td style="width:5%;"><?php echo $row["sf"] ?>
                            </td>
                            <td style="width:5%;"><?php echo $row["city"] ?>
                            </td>
                            <td style="width:7.5%;"><?php echo $row["xian"] ?>
                            </td>
                            <td style="width:7.5%;"><?php echo $row["dizhi"] ?>
                            </td>
                
                            <td style="width:7.5%;">
                              <?php 
                                  switch ($row["status"]) {
                                      case 0:
                                          echo "未处理";
                                          break;
                                      case 1:
                                          echo "已下单";
                                          break;
                                      case 2:
                                          echo "已发货";
                                          break;
                                      case 3:
                                          echo "已完成";
                                          break;
                                      default:
                                          echo "开卡失败";
                                          break;
                                  } 
                              ?>
                            </td>
                            
                            <td style="width:7.5%;">
                              <?php 
                                  switch ($row["card_sta"]) {
                                      case 0:
                                          echo "未激活";
                                          break;
                                      case 1:
                                          echo "已激活";
                                          break;
                                      default:
                                            echo "未知状态";
                                            break;
                                  } 
                              ?>
                            </td>

    
                            <td style="width:7.5%;"><?php echo $row["beizhu"] ?>
                            </td>
                            <td style="width:10%;">
                             <?php 
                             $date = date('Y-m-d H:i:s', $row["time"]);
                              echo $date; // 输出：2021-07-22 16:52:07
                             ?>
                            </td>
                            <td style="width:15%;">
                              <a href=updatesta.php?id=<?php echo $row['id'] ?> class="btn btn-danger btn-sm">处理</a>
                
                              <a href="postapi.php?id=<?php echo $row['id'] ?>"  class="btn btn-success btn-sm">提审</a>
                            </td>
                          
                          </tr>
                        <?php
                        }
                        ?>
                      </tbody>
                      <tfoot>
                        <tr>
                          <td colspan="12" style="text-align: right;">
                        <?php
                        echo "<p>共 $maxpage 页&nbsp;&nbsp;";
                        echo "每页 $page_size 项&nbsp;&nbsp;";
                        //设置上一页
                        if ($page > 1) {
                          $prepage = $page - 1;
                          echo "<a href='?curpage=$prepage'>上一页</a>&nbsp;&nbsp;";
                        }
                        //设置下一页
                        if ($page < $maxpage) {
                          $nextpage = $page + 1;
                          echo "<a href='?curpage=$nextpage'>下一页</a>&nbsp;&nbsp;";
                        }
                        echo "&nbsp;&nbsp;第 $page 页</p>";
                      }
                    }
                  ?>
                        </td>
                      </tr>
                    </tfoot>
                </table>

                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    </div>
    <script src="../merchant/assets/js/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('.update-btn').click(function(e){
                e.preventDefault();
                var id = $(this).data('id');
                $.ajax({
                    url: 'updatesta.php',
                    type: 'GET',
                    data: {id: id},
                    success: function(data){
                        alert('状态处理成功！');
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                        console.log(textStatus);
                    }
                });
            });
        });

      $(document).ready(function() {
        $('.dropdown-menu a').click(function(e) {
          e.preventDefault();
          var field = $(this).data('field');
          var buttonText = $(this).text();
          $('#search').val(field);
          $('#search-btn').html(buttonText + ' <span class="caret"></span>');
        });
      });
    </script>

  </main>
  <!--End 页面主要内容-->
  <?php require_once('common/footer.php'); ?>
<?php
} else {
  echo "<script>alert('您尚未登录，没有权限访问该页面');location.href='login.php';</script>";
}
